﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ConnectToDatabase;
using ConnectToDatabase.Model;

namespace QLGiayDep.Forms
{
    /// <summary>
    /// Interaction logic for FormHoaDon.xaml
    /// </summary>
    public partial class FormHoaDon : Page
    {
        SelectDB selectDB;
        InsertDB insertDB;
        DeleteDB deleteDB;
        SetID setID;
        UpdateDB updateDB;
        int m_iFrom = 0;
        int m_iTo = 12;
        int m_iDistance = 0;
        DateTime m_dtNow = DateTime.Now;
        static bool m_bSelectIndex = false;
        static int m_iTongTien = 0;
        DateTime m_dtmTemp = new DateTime();
        int m_iLoaiKH = 0;
        KhuyenMai m_khuyenMai = new KhuyenMai();
        string m_sMaNV;
        string URL;

        public FormHoaDon()
        {
            InitializeComponent();

            updateDB = (UpdateDB)Activator.GetObject(typeof(UpdateDB), URL + "UPDATEDB_OBJECT");
            insertDB = (InsertDB)Activator.GetObject(typeof(InsertDB), URL + "INSERTDB_OBJECT");
            selectDB = (SelectDB)Activator.GetObject(typeof(SelectDB), URL + "SELECTDB_OBJECT");
            deleteDB = (DeleteDB)Activator.GetObject(typeof(DeleteDB), URL + "DELETEDB_OBJECT");
            setID = (SetID)Activator.GetObject(typeof(SetID), URL + "SETID_OBJECT");

            txtMaHoaDon.Text = setID.GenerateMaHoaDon();
            m_iDistance = m_iTo - m_iFrom + 1;
            dtmNgayBan.SelectedDate = DateTime.Now;
            AddComboboxPagingSPMua(cmdPagingSanPhamChuaMua, selectDB.SelectAllSanPhamMuaBan().Count);
            dataDSSP.ItemsSource = selectDB.SelectAllSanPhamMuaBan(m_iFrom, m_iTo);
            m_dtmTemp = dtmNgayBan.SelectedDate.Value;
            if (radYes.IsChecked == true)
                m_iLoaiKH = 1;
            insertDB.InsertHoaDon(txtMaHoaDon.Text, m_dtmTemp.ToString("yyyy/MM/dd"), 0, m_iLoaiKH, txtMaNVBanHang.Text);
        }

        public FormHoaDon(string m_sUserName, string _URL)
        {
            InitializeComponent();

            URL = _URL;

            updateDB = (UpdateDB)Activator.GetObject(typeof(UpdateDB), URL + "UPDATEDB_OBJECT");
            insertDB = (InsertDB)Activator.GetObject(typeof(InsertDB), URL + "INSERTDB_OBJECT");
            selectDB = (SelectDB)Activator.GetObject(typeof(SelectDB), URL + "SELECTDB_OBJECT");
            deleteDB = (DeleteDB)Activator.GetObject(typeof(DeleteDB), URL + "DELETEDB_OBJECT");
            setID = (SetID)Activator.GetObject(typeof(SetID), URL + "SETID_OBJECT");

            m_sMaNV = m_sUserName;
            txtMaNVBanHang.Text = m_sUserName;
            txtMaHoaDon.Text = setID.GenerateMaHoaDon();
            m_iDistance = m_iTo - m_iFrom + 1;
            dtmNgayBan.SelectedDate = DateTime.Now;
            AddComboboxPagingSPMua(cmdPagingSanPhamChuaMua, selectDB.SelectAllSanPhamMuaBan().Count);

            URL = _URL;
            selectDB = (SelectDB)Activator.GetObject(typeof(SelectDB), URL + "SELECTDB_OBJECT");
            dataDSSP.ItemsSource = selectDB.SelectAllSanPhamMuaBan(m_iFrom, m_iTo);
            m_dtmTemp = dtmNgayBan.SelectedDate.Value;
            if (radYes.IsChecked == true)
                m_iLoaiKH = 1;
            insertDB.InsertHoaDon(txtMaHoaDon.Text, m_dtmTemp.ToString("yyyy/MM/dd"), 0, m_iLoaiKH, txtMaNVBanHang.Text);
        }

        private void AddComboboxPagingSPMua(ComboBox m_cmbTemp, int m_iCountTemp)
        {
            m_cmbTemp.Items.Clear();
            int count = m_iCountTemp;

            int temp = count / m_iDistance;
            if (temp * m_iDistance < count)
                temp++;

            for (int i = 0; i < temp; i++)
            {
                m_cmbTemp.Items.Add(i + 1);
            }
        }

        private void btnSearchMaSPMua_Click(object sender, RoutedEventArgs e)
        {
            if (selectDB.SelectAllSanPhamMuaBan_MaSP(txtMaSPMua.Text) == null)
                MessageBox.Show("Không tìm thấy sản phẩm", "Thông Báo");
            else
            {
                dataDSSP.ItemsSource = selectDB.SelectAllSanPhamMuaBan_MaSP(txtMaSPMua.Text);
                btnThemSPMua.IsEnabled = true;
            }
        }

        private void cmdPagingSanPhamChuaMua_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            txtMaSPMua.Clear();
            if (!m_bSelectIndex)
            {
                int temp = int.Parse(cmdPagingSanPhamChuaMua.SelectedItem.ToString()) - 1;
                int sumrows = selectDB.SelectAllDSSanPham().Count;
                if (m_iTo + m_iDistance * temp > sumrows)
                    dataDSSP.ItemsSource = selectDB.SelectAllSanPhamMuaBan(m_iFrom + m_iDistance * temp, sumrows - 1);
                else
                    dataDSSP.ItemsSource = selectDB.SelectAllSanPhamMuaBan(m_iFrom + m_iDistance * temp, m_iTo + m_iDistance * temp);
            }
            else
                m_bSelectIndex = false;
        }

        private void btnThemSPMua_Click(object sender, RoutedEventArgs e)
        {
            if (txtMaSPMua.Text == string.Empty)
                MessageBox.Show("Chưa nhập mã sản phẩm", "Thông Báo");
            else
            {
                if (selectDB.SelectAllSanPham(txtMaSPMua.Text).TinhTrang == 1)
                {
                    if (radYes.IsChecked == true)
                        m_iLoaiKH = 1;

                    string m_sMaNhomSP = selectDB.SelectAllSanPham(txtMaSPMua.Text).MaNhomSanPham.ToString();
                    int m_iSoLuongNhomSPHienTai = selectDB.SelectShoLuongNhomSP(m_sMaNhomSP);

                    insertDB.InsertCTHoaDon(txtMaHoaDon.Text, txtMaSPMua.Text, selectDB.SelectAllSanPhamMuaBan_MaSP(txtMaSPMua.Text)[0].GiaBan);

                    updateDB.UpdateSanPham(txtMaSPMua.Text, 0);

                    m_iTongTien = m_iTongTien + selectDB.SelectAllSanPhamDaMua(txtMaHoaDon.Text, txtMaSPMua.Text)[0].GiaBan;

                    txtTongTien.Text = m_iTongTien.ToString();

                    updateDB.UpdateHoaDon(txtMaHoaDon.Text, m_dtmTemp.ToString("yyyy/MM/dd"), m_iTongTien, m_iLoaiKH, txtMaNVBanHang.Text);

                    updateDB.UpdateNhomSanPham(m_sMaNhomSP, m_iSoLuongNhomSPHienTai - 1);

                    txtMaSPMua.Clear();
                    
                    dataHangDaMua.ItemsSource = selectDB.SelectAllSanPhamDaMua(txtMaHoaDon.Text);

                    dataDSSP.ItemsSource = selectDB.SelectAllSanPhamMuaBan(m_iFrom, m_iTo);
                }
                else
                    MessageBox.Show("Sản Phẩm Đã Được Bán", "Thông Báo");
            }
        }

        private void btnTimTrongGioHang_Click(object sender, RoutedEventArgs e)
        {
            if (selectDB.SelectAllSanPhamDaMua(txtMaHoaDon.Text, txtSPTrongGioHang.Text) == null)
                MessageBox.Show("Không tìm thấy sản phẩm", "Thông Báo");
            else
            {
                dataHangDaMua.ItemsSource = selectDB.SelectAllSanPhamDaMua(txtMaHoaDon.Text, txtSPTrongGioHang.Text);
                btnGiamSPMua.IsEnabled = true;
            }
        }

        private void btnGiamSPMua_Click(object sender, RoutedEventArgs e)
        {
            string m_sMaNhomSP = selectDB.SelectAllSanPham(txtSPTrongGioHang.Text).MaNhomSanPham;
            int m_iSoLuongNhomSPHienTai = selectDB.SelectShoLuongNhomSP(m_sMaNhomSP);

            deleteDB.DeleteCT_HoaDon(txtMaHoaDon.Text, txtSPTrongGioHang.Text);
            updateDB.UpdateSanPham(txtSPTrongGioHang.Text, 1);
            updateDB.UpdateNhomSanPham(m_sMaNhomSP, m_iSoLuongNhomSPHienTai + 1);
            m_iTongTien = m_iTongTien - selectDB.SelectAllSanPhamDaMua(txtMaHoaDon.Text, txtSPTrongGioHang.Text)[0].GiaBan;

            txtTongTien.Text = m_iTongTien.ToString();
            updateDB.UpdateHoaDon(txtMaHoaDon.Text, m_dtmTemp.ToString("yyyy/MM/dd"), m_iTongTien, m_iLoaiKH, txtMaNVBanHang.Text);

            dataHangDaMua.ItemsSource = selectDB.SelectAllSanPhamDaMua(txtMaHoaDon.Text);
            dataDSSP.ItemsSource = selectDB.SelectAllSanPhamMuaBan(m_iFrom, m_iTo);

            txtSPTrongGioHang.Clear();
        }

        private void btnThanhTien_Click(object sender, RoutedEventArgs e)
        {
            btnKetThuc.IsEnabled = true;
            if (radYes.IsChecked == true)
                txtThanhTien.Text = (int.Parse(txtTongTien.Text) * 0.9).ToString();
            else
                txtThanhTien.Text = txtTongTien.Text;
        }

        private void btnKetThuc_Click(object sender, RoutedEventArgs e)
        {
            if(radYes.IsChecked == true)
                m_iLoaiKH = 1;
            int kt = updateDB.UpdateHoaDon(txtMaHoaDon.Text, m_dtNow.ToString("yyyy/MM/dd"), int.Parse(txtThanhTien.Text), m_iLoaiKH, txtMaNVBanHang.Text);
            if (kt == -1)
                MessageBox.Show("Thất bại","Thông báo");
            else
                MessageBox.Show("Tính tiền thành công", "Thông báo");
        }

        private void btnHoaDonMoi_Click(object sender, RoutedEventArgs e)
        {
            txtMaNVBanHang.Text = m_sMaNV;
            txtMaHoaDon.Text = setID.GenerateMaHoaDon();
            AddComboboxPagingSPMua(cmdPagingSanPhamChuaMua, selectDB.SelectAllSanPhamMuaBan().Count);
            dataDSSP.ItemsSource = selectDB.SelectAllSanPhamMuaBan(m_iFrom, m_iTo);
            m_dtmTemp = dtmNgayBan.SelectedDate.Value;
            txtTongTien.Clear();
            txtThanhTien.Clear();
            dataHangDaMua.ItemsSource = null;
            insertDB.InsertHoaDon(txtMaHoaDon.Text, m_dtmTemp.ToString("yyyy/MM/dd"), 0, m_iLoaiKH, txtMaNVBanHang.Text);
        }

    }
}
